package com.atguigu.gmall.oms.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
    //业务队列交换机绑定： 保存"order.exchange" , "order.delay.close",消息
    // 消息设置过期时间：延迟队列
    @Bean
    public Exchange orderExchange(){
        return ExchangeBuilder.topicExchange("order.exchange")
                .durable(true)
                .ignoreDeclarationExceptions()
                .build();
    }
    @Bean
    public Queue orderDelayColseQueue(){
        return QueueBuilder.durable("order.delay.close.queue")
//                .ttl(2*60000)//过期时间半小时
                .ttl(30*60000)//过期时间半小时
                .deadLetterExchange("order.delay.exchange")
                .deadLetterRoutingKey("order.delay.msg")
                .build();
    }
    @Bean
    public Binding orderDelayBinding(Exchange orderExchange,Queue orderDelayColseQueue){
        return BindingBuilder.bind(orderDelayColseQueue)
                .to(orderExchange)
                .with("order.delay.close")
                .noargs();
    }
    //死信队列 死信交换 绑定：
    @Bean
    public Exchange deadOrderExchange(){
        return ExchangeBuilder.topicExchange("order.delay.exchange")
                .durable(true)
                .ignoreDeclarationExceptions()
                .build();
    }
    @Bean
    public Queue deadOrderDelayColseQueue(){
        return QueueBuilder.durable("order.dead.close.queue")
                .build();
    }
    @Bean
    public Binding deadOrderDelayBinding(Exchange deadOrderExchange,Queue deadOrderDelayColseQueue){
        return BindingBuilder.bind(deadOrderDelayColseQueue)
                .to(deadOrderExchange)
                .with("order.delay.msg")
                .noargs();
    }

}
